package demo.ybt;

public class P1262 {

    public static void main(String[] args) {
        P1262 p1262 = new P1262();
        int res = p1262.res(new int[]{5, 10, 20, 5, 4, 5}, new int[][]{{1, 2}, {1, 4}, {2, 4}, {3, 4}, {4, 5}, {4, 6}, {5, 6}});
        System.out.println(res);
    }

    public int res(int[] vs, int[][] path) {
        //5 10 20 5 4 5
        //
        int dp[] = new int[vs.length + 1];
        for (int i = 0; i < vs.length; i++) {
            dp[i + 1] = vs[i];
        }

        for (int i = 0; i < path.length; i++) {
            int befor = path[i][0];
            int behind = path[i][1];
            dp[behind] = Math.max(dp[behind], dp[befor] + vs[behind - 1]);
        }
        return dp[vs.length];
    }
}
